
import { JSEncrypt } from 'jsencrypt'
import CryptoJS from 'crypto-js'


const pub = '-----BEGIN PUBLIC KEY-----' +
	'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwQVZYVyygV5gQRBOjrPtQ9bCOcWuEKugTengddZfSCONnOdufCpMJcAAB7YiscEsy3nJnn8dZGgKMO8IpcQjnmb++uVSC8ccRkHE2Rsp9SN1Kf7B6witjkFURABtRTfNGzu/CRoj8oIVC1kU4KgRuaXstt/17qvF3I5mlfhamRQIDAQAB' +
	'-----END PUBLIC KEY-----'
const priv = '-----BEGIN RSA PRIVATE KEY-----' +
	'MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIk0A9Al/J0/oMlns15wMsZscF3PMwxjRZIABddXgHdkeTPIIXYWTK9ybTMJ0mP2nYm0QE1XzxOaYx6GT2uyuHvRplyQQWIIlLmgCsLa1z5fLpDWzvw1tE7XCHj924DN9mCyxlKqWfZfUsIGHzRoZqIYsTn9qA2mQrUXsdwVDjDnAgMBAAECgYBhp4oHPuJcMguJDqxWTVl2bXxU6MMW0BmWXznJGdiwIe06c/mNCTYvsiwTJax/xeo/NvKFmjgjZcGi2knxB9XSD+49Q6h3oqn3zACUhkXBpPCxzJfTuQgKEr4ifnImLRkr4Uv1ALLFQvnJebJx4paz8px5NwguxOHoBHXbhK3QUQJBAMi1D5bvrivhuD3RZhyi9axredLLjTNfQ15UzQMTtDHqTQf8IpHECCDMZReunKLb85MYdyq8EN/yP5B+WlgihdMCQQCvAE9HjHOZx2s0zVd7DLyjEzlGzmIsUVyXy+lgtrd7/TAG/dRlgj1JMCRL7OEyhabR/Uhod1JOmmmSrA86+dgdAkBBk88tLNDGq7CMGi/36VLF7DPYqQNQpq6XUrPNbpGwwXI7yqjs4ZUaSk45h6bMG3uhMxw0jVAiK7+TEYWpSFZbAkApRjhslpq5PUhytq+YTQO8ro8GvSx4aWQeRTCw58dUzf1i2MEcg+Ayf9E/uwnOIambw1ItmV7nPRIDTuN0tHKdAkAVN2p946xR3yVaFKXQ1Hwf21MlgBSEwFsoUWZwA6Ycio+1sOTh057B4+CjUy57/rXXE1FPECKrNfCE6PnVmzLq' +
	'-----END RSA PRIVATE KEY-----'
// aes生成秘钥偏移 
// const iv = CryptoJS.enc.Latin1.parse('0123456789ABCDEF')

const iv = CryptoJS.enc.Utf8.parse('0123456789ABCDEF') // 十六位十六进制数作为密钥偏移量

function getAesKey(num) {
	let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	let key = "";
	for (var i = 0; i < num; i++) {
		let randomPoz = Math.floor(Math.random() * library.length);
		key += library.substring(randomPoz, randomPoz + 1);
	}

	return key

}

//aes解密方法
function aesDecrypt(word, key) {

	
	let decrypt = CryptoJS.AES.decrypt(word, CryptoJS.enc.Utf8.parse(key), { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
	let decString = CryptoJS.enc.Utf8.stringify(decrypt).toString()
	if (decString.charAt(0) === '{' || decString.charAt(0) === '[') {
	        // JSON.parse
	  decString = JSON.parse(decString)
	}
	return decString
	

}
//aes加密方法
function aesEncrypt(word, key) {
	
		
	if (word instanceof Object) {
	        // JSON.stringify
	  word = JSON.stringify(word)
	}
	let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(word), CryptoJS.enc.Utf8.parse(key), { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
	return encrypted.toString()

}

//rsa加密
function EncryptNode(data) {
	var en = new JSEncrypt()
	en.setPublicKey(pub)
	let encrypted = en.encrypt(data)
	return encrypted
}
//rsa解密
function DecryptNode(data) {
	var decrypt = new JSEncrypt()
	decrypt.setPrivateKey(priv)
	let dncrypted = decrypt.decrypt(data)
	return dncrypted
}


// function EncryptNode(data) {
// 	let pubKey = forge.pki.publicKeyFromPem(pub)
// 	return base64_encode(pubKey.encrypt(forge.util.encodeUtf8(data), 'RSAES-OAEP', {
// 		md: forge.md.sha256.create(),
// 		mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
// 	}))
// }

// function DecryptNode(data) {
// 	let privateKey = forge.pki.privateKeyFromPem(priv)
// 	return forge.util.decodeUtf8(privateKey.decrypt(atob(data), 'RSAES-OAEP', {
// 		md: forge.md.sha256.create(),
// 		mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
// 	}))
// }

export {
	getAesKey,
	aesDecrypt,
	aesEncrypt,
	DecryptNode,
	EncryptNode
}
